// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 

package com.yoyo.lib.net.callback;

import android.net.ParseException;
import android.text.TextUtils;
import com.yoyo.lib.AppException;
import com.yoyo.lib.AppException.ErrorType;
import com.yoyo.lib.UserInfo;
import com.yoyo.lib.net.itf.ICallback;
import com.yoyo.lib.net.itf.IProgressListener;
import com.yoyo.lib.utilities.IOUtilities;
import java.io.*;
import java.net.HttpURLConnection;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
import org.apache.http.*;
import org.apache.http.util.EntityUtils;

public abstract class AbstractCallback<T> implements ICallback<T> {

    private static final int IO_BUFFER_SIZE = 4 * 1024;
    private boolean isCancelled = false;
    protected Class<T> mReturnType;
    protected String path;

    protected T bindData(String res, IProgressListener task) throws AppException {
        checkIsCancelled();
        return null;
    }

    @Override
    public Object handleConnection(HttpURLConnection httpurlconnection) throws AppException {
        return handleConnection(httpurlconnection, null);
    }

    @Override
    public Object handleConnection(HttpURLConnection httpurlconnection, IProgressListener iprogresslistener)
            throws AppException {
        /*int i;
        long l;
        checkIsCancelled();
        FileNotFoundException filenotfoundexception;
        UserInfo userinfo1;
        AppException appexception1;
        try
        {
            i = httpurlconnection.getResponseCode();
        }
        catch(IOException ioexception)
        {
            throw new AppException(ErrorType.ConnectionException, "IOException", ioexception.getMessage(), null);
        }
        l = httpurlconnection.getContentLength();
        i;
        JVM INSTR tableswitch 200 201: default 40
        //                   200 138
        //                   201 398;
           goto _L1 _L2 _L3
        _L1:
        String s;
        Object obj;
        FileOutputStream fileoutputstream;
        byte abyte0[];
        int j;
        int k;
        try
        {
            userinfo1 = new UserInfo();
            userinfo1.putInfo("cloud_response_status_code", Integer.valueOf(i));
            userinfo1.putInfo("cloud_response_body", IOUtilities.readStreamToMemory(httpurlconnection.getErrorStream()));
            appexception1 = new AppException(com.stay.lib.AppException.ErrorType.CloudException, "CloudException", httpurlconnection.getResponseMessage(), userinfo1);
            throw appexception1;
        }
        // Misplaced declaration of an exception variable
        catch(FileNotFoundException filenotfoundexception)
        {
            throw new AppException(com.stay.lib.AppException.ErrorType.FileException, "FileNotFoundException", filenotfoundexception.getMessage(), null);
        }
        catch(IOException ioexception1)
        {
            throw new AppException(com.stay.lib.AppException.ErrorType.ConnectionException, "IOException", ioexception1.getMessage(), null);
        }
        _L2:
        s = httpurlconnection.getContentEncoding();
        if(s == null || !"gzip".equalsIgnoreCase(s)) goto _L5; else goto _L4
        _L4:
        obj = new GZIPInputStream(httpurlconnection.getInputStream());
        _L19:
        if(TextUtils.isEmpty(path)) goto _L7; else goto _L6
        _L6:
        fileoutputstream = new FileOutputStream(path);
        abyte0 = new byte[4096];
        j = 0;
        _L14:
        k = ((InputStream) (obj)).read(abyte0);
        if(k == -1) goto _L9; else goto _L8
        _L8:
        checkIsCancelled();
        if(iprogresslistener == null) goto _L11; else goto _L10
        _L10:
        j += k;
        if(l == -1L) goto _L13; else goto _L12
        _L12:
        iprogresslistener.progressChanged(1, (int)((long)(j * 100) / l), null);
        _L11:
        fileoutputstream.write(abyte0, 0, k);
          goto _L14
        _L5:
        if(s == null) goto _L16; else goto _L15
        _L15:
        if("deflate".equalsIgnoreCase(s))
        {
            obj = new InflaterInputStream(httpurlconnection.getInputStream());
            continue;  Loop/switch isn't completed 
        }
        _L16:
        obj = httpurlconnection.getInputStream();
        continue;  Loop/switch isn't completed 
        _L13:
        iprogresslistener.progressChanged(1, j / 1024, null);
          goto _L11
        _L9:
        Object obj1;
        fileoutputstream.flush();
        fileoutputstream.close();
        ((InputStream) (obj)).close();
        obj1 = bindData(path, iprogresslistener);
          goto _L17
        _L7:
        obj1 = bindData(IOUtilities.readStreamToMemory(((InputStream) (obj))), iprogresslistener);
          goto _L17
        _L3:
        UserInfo userinfo = new UserInfo();
        userinfo.putInfo("cloud_response_status_code", Integer.valueOf(i));
        userinfo.putInfo("cloud_response_body", httpurlconnection.getResponseMessage());
        AppException appexception = new AppException(com.stay.lib.AppException.ErrorType.CloudException, "HttpStatus.SC_CREATED", httpurlconnection.getResponseMessage(), userinfo);
        throw appexception;
        _L17:
        return obj1;
        if(true) goto _L19; else goto _L18
        _L18:*/
        return null;
    }

    @Override
    public Object handleResponse(HttpResponse httpresponse) throws AppException {
        return handleResponse(httpresponse, null);
    }

    @Override
    public Object handleResponse(HttpResponse response, IProgressListener task) throws AppException {
        checkIsCancelled();
        try {
            int statusCode = response.getStatusLine().getStatusCode();
            HttpEntity httpEntity = response.getEntity();
            long contentLength = httpEntity != null ? httpEntity.getContentLength() : 0;
            if (statusCode == HttpStatus.SC_OK) {
                // if set path, write the data into file
                if (!TextUtils.isEmpty(path)) {
                    FileOutputStream fos = new FileOutputStream(path);
                    InputStream is = null;
                    String encoding = httpEntity.getContentEncoding().getValue();
                    if (null != encoding && "gzip".equalsIgnoreCase(encoding)) {
                        is = new GZIPInputStream(httpEntity.getContent());
                    } else if (null != encoding && "deflate".equalsIgnoreCase(encoding)) {
                        is = new InflaterInputStream(httpEntity.getContent());
                    } else {
                        is = httpEntity.getContent();
                    }
                    byte[] b = new byte[IO_BUFFER_SIZE];
                    int curPos = 0;
                    int read;
                    while ((read = is.read(b)) != -1) {
                        checkIsCancelled();
                        if (task != null) {
                            curPos += read;
                            if (contentLength != -1) {
                                task.progressChanged(IProgressListener.DOWNLOADING,
                                        ( int ) (curPos * 100 / contentLength), null);
                            } else {
                                task.progressChanged(IProgressListener.DOWNLOADING, curPos / 1000, null);
                            }
                        }
                        fos.write(b, 0, read);
                    }
                    fos.flush();
                    fos.close();
                    is.close();
                    return bindData(path, task);
                } else {
                    return bindData(null, task);
                }
            } else {
                UserInfo errorInfo = new UserInfo();
                if (httpEntity != null) {
                    errorInfo.putInfo(UserInfo.CLOUD_RESPONSE_BODY, EntityUtils.toString(httpEntity));
                }
                errorInfo.putInfo(UserInfo.CLOUD_RESPONSE_STATUS_CODE, statusCode);
                return new AppException(ErrorType.CloudException, "response code is not 200", response
                        .getStatusLine().getReasonPhrase(), errorInfo);
            }
        } catch (FileNotFoundException e) {
            throw new AppException(ErrorType.FileException, "FileNotFoundException", e.getMessage(), null);
        } catch (IllegalStateException e) {
            throw new AppException(ErrorType.ConnectionException, "IllegalStateException", e.getMessage(),
                    null);
        } catch (ParseException e) {
            throw new AppException(ErrorType.ConnectionException, "ParseException", e.getMessage(),
                    null);
        } catch (IOException e) {
            throw new AppException(ErrorType.ConnectionException, "IOException", e.getMessage(), null);
        }

    }

    public void cancel() {
        isCancelled = true;
    }

    public void checkIsCancelled() throws AppException {
        if (isCancelled)
            throw new AppException(ErrorType.CancelException, "task cancel",
                    "the request has been cancelled", null);
        else
            return;
    }
    
    public AbstractCallback setFilePath(String s) {
        path = s;
        return this;
    }

    public AbstractCallback setReturnType(Class class1) {
        mReturnType = class1;
        return this;
    }

}
